package com.ruoyi.patrol.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.PtSchool;
import com.ruoyi.patrol.service.IPtSchoolService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.domain.PtSchoolGroup;
import com.ruoyi.patrol.service.IPtSchoolGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校小组Controller
 *
 * @author lkm
 * @date 2023-05-13
 */
@RestController
@RequestMapping("/patrol/group")
public class PtSchoolGroupController extends BaseController {
    @Autowired
    private IPtSchoolGroupService ptSchoolGroupService;

    @Autowired
    private IPtSchoolService ptSchoolService;

    /**
     * 查询学校小组列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtSchoolGroup ptSchoolGroup) {
        startPage();
        List<PtSchoolGroup> list = ptSchoolGroupService.selectPtSchoolGroupList(ptSchoolGroup);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('patrol:group:list')")
    @GetMapping("/schoollist")
    public TableDataInfo schoollist() {
        Long schoolId = SecurityUtils.getSchoolId();
        List list;
        if (schoolId != null) {
            PtSchool ptSchool = ptSchoolService.selectPtSchoolById(schoolId);
            list = new ArrayList();
            list.add(ptSchool);
        } else list = ptSchoolService.selectPtSchoolList(null);
        return getDataTable(list);
    }


    /**
     * 导出学校小组列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:group:export')")
    @Log(title = "学校小组", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtSchoolGroup ptSchoolGroup) {
        List<PtSchoolGroup> list = ptSchoolGroupService.selectPtSchoolGroupList(ptSchoolGroup);
        ExcelUtil<PtSchoolGroup> util = new ExcelUtil<PtSchoolGroup>(PtSchoolGroup.class);
        return util.exportExcel(list, "group");
    }

    /**
     * 获取学校小组详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:group:query')")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId) {
        return AjaxResult.success(ptSchoolGroupService.selectPtSchoolGroupById(groupId));
    }

    /**
     * 新增学校小组
     */
    @PreAuthorize("@ss.hasPermi('patrol:group:add')")
    @Log(title = "学校小组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtSchoolGroup ptSchoolGroup) {
        int rows = ptSchoolGroupService.insertPtSchoolGroup(ptSchoolGroup);
        if (rows == -3)
            return AjaxResult.error("小组名称重复");
        return toAjax(rows);
    }

    /**
     * 修改学校小组
     */
    @PreAuthorize("@ss.hasPermi('patrol:group:edit')")
    @Log(title = "学校小组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtSchoolGroup ptSchoolGroup) {
        int rows = ptSchoolGroupService.updatePtSchoolGroup(ptSchoolGroup);
        if (rows == -3)
            return AjaxResult.error("小组名称重复");
        return toAjax(rows);
    }

    /**
     * 删除学校小组
     */
    @PreAuthorize("@ss.hasPermi('patrol:group:remove')")
    @Log(title = "学校小组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds) {
        return toAjax(ptSchoolGroupService.deletePtSchoolGroupById(groupIds[0]));
    }

    @GetMapping("/optionselect")
    public AjaxResult optionselect(PtSchoolGroup ptSchoolGroup) {
//        if (ptSchoolGroup.getGroupSchoolId() == null) {
//            ptSchoolGroup.setGroupSchoolId(SecurityUtils.getSchoolId());
//        }
        List<PtSchoolGroup> list = ptSchoolGroupService.selectPtSchoolGroupList(ptSchoolGroup);
        return AjaxResult.success(list);
    }
}
